package com.xyy.saas.payment.common.pool;

import java.util.concurrent.Callable;

/**
 * @author liujf
 * 建议与AlarmRejectHandler 一起食用，任务告警准确报出具体任务
 */
public class NamedTask implements Runnable, Callable<Object> {
    private Runnable task;
    private Callable callable;
    private String name;

    private NamedTask (Runnable task, String name) {
        this.task = task;
        this.name = name;
    }

    private NamedTask (Callable task, String name) {
        this.callable = task;
        this.name = name;
    }

    public static Runnable createRun(Runnable task, String name) {
        return new NamedTask(task,name);
    }

    public  static <T> Callable<T> createCall(Callable<T> callable, String name) {
        return (Callable<T>) new NamedTask(callable,name);
    }

    @Override
    public void run() {
        task.run();
    }

    @Override
    public String toString() {
        return this.name;
    }

    @Override
    public Object call() throws Exception {
        return callable.call();
    }
}
